/* Constants */
LIST_DIR_H							= 1
LIST_DIR_V							= 2

/* Constructor */
BelListBox							= {}

/* Initialization */
BelListBox.Initialize				= function( self, ... )

	/* Base stuff */
	BelFrame.Initialize( self, ... )
	
	/* Default values */
	self.IncrustBorder				= false
	self.ListItems					= {}
	self.Direction					= LIST_DIR_V
	self.Padding					= { l=16, r=16, t=32, b=16 }
	self.Spacing					= 4
	self.ControlMaxSize				= { w=ScrW() / 15, h=ScrH() / 15 }
	self.ScrollAreaSize				= { w=32, h=32 }
	
	/* Contents panel */
	self.Contents					= vgui.Create( "BelPanel", self )
	self.Contents:Initialize()
	self.Contents.CornerSizeRatio	= 0
	self.Contents.DropShadow		= false
	self.Contents.WaitForChildren	= false
	self.Contents.SplitDesign		= false
	self.Contents:SetMaxWide( self:GetMaxWide() - self.Padding.l - self.Padding.r - self.ScrollAreaSize.w )
	self.Contents:SetMaxTall( self:GetMaxTall() - self.Padding.t - self.Padding.b )
	self.Contents:SetFinalPos( self.Padding.l, self.Padding.t )
	self.Contents:SetBackgroundColors(
		Color( 20, 20, 20, 200 ),
		Color( 40, 40, 40, 200 ),
		Color( 40, 40, 40, 200 )
	)
	
	/* Scroll panel */
	self.ScrollPanel				= vgui.Create( "BelPanel", self.Contents )
	self.ScrollPanel:Initialize()
	self.ScrollPanel.CornerSizeRatio = 0
	self.ScrollPanel.SplitDesign	= false
	self.ScrollPanel.Padding		= { l=4, r=4, t=4, b=4 }
	self.ScrollPanel:SetFinalPos( 0, 0 )
	self.ScrollPanel.DropShadow		= false
	self.ScrollPanel:SetBackgroundColors(
		Color( 20, 20, 20, 200 ),
		Color( 40, 40, 40, 200 ),
		Color( 40, 40, 40, 200 )
	)
	
	/* Scrollbar */
	self.Scrollbar					= vgui.Create( "BelScrollbar", self )
	self.Scrollbar:Initialize()
	self.Scrollbar.Direction		= LIST_DIR_V
	
	/* Sync the scrollbar */
	--self.Scrollbar:SetDirection( LIST_DIR_V )
	self.Scrollbar:UpdateLayout()
end

/* Add control */
BelListBox.AddControl				= function( self, control )
	
	/* Unsure the control is valid */
	if not IsPanel( control ) then
		DEBUG:ErrMsg( "The given control is not valid.", "BelListBox.AddControl" )
		return
	end
	
	/* Insert into items list */
	table.insert( self.ListItems, control )
	
	/* Add child if not done yet by BelControls */
	BelControls.AddChild( self.ScrollPanel, control )
	
	/* Update the layout */
	self:UpdateLayout()
end

/* Update the layout based on direction */
BelListBox.UpdateLayout				= function( self )

	/* Update layout of controls separately */
	for i,v in ipairs( self.ListItems ) do
		self:UpdateControlLayout( v, i )
	end
	
	/* Update contents */
	if self.Direction == LIST_DIR_V then
		self.Contents:SetMaxWide( self:GetMaxWide() - self.Padding.l - self.Padding.r - self.ScrollAreaSize.w )
		self.Contents:SetMaxTall( self:GetMaxTall() - self.Padding.t - self.Padding.b )
	else
		self.Contents:SetMaxWide( self:GetMaxWide() - self.Padding.l - self.Padding.r )
		self.Contents:SetMaxTall( self:GetMaxTall() - self.Padding.t - self.Padding.b - self.ScrollAreaSize.h )
	end
	self.Contents:SetFinalPos( self.Padding.l, self.Padding.t )
	
	/* Update scroll panel */
	if self.Direction == LIST_DIR_V then
		self.ScrollPanel:SetMaxWide( self.Contents:GetMaxWide() )
		self.ScrollPanel:SetMaxTall( math.max( self:GetContentsSize(), self.Contents:GetMaxTall() ) )
	else
		self.ScrollPanel:SetMaxTall( self.Contents:GetMaxTall() )
		self.ScrollPanel:SetMaxWide( math.max( self:GetContentsSize(), self.Contents:GetMaxWide() ) )
	end
end

BelListBox.UpdateControlLayout		= function( self, control, index )

	/* Set the size */
	if self.Direction == LIST_DIR_V then
		control:SetMaxWide( self.Contents:GetMaxWide() - self.ScrollPanel.Padding.l - self.ScrollPanel.Padding.r )
		control:SetMaxTall( self.ControlMaxSize.h )
	else
		control:SetMaxTall( self.Contents:GetMaxTall() - self.ScrollPanel.Padding.t - self.ScrollPanel.Padding.b )
		control:SetMaxWide( self.ControlMaxSize.w )
	end
	
	/* Set the position */
	if self.Direction == LIST_DIR_V then
		control.FinalPosition.x		= self.ScrollPanel.Padding.l
		control.FinalPosition.y		= ( self.ControlMaxSize.h + self.Spacing ) * ( index - 1 ) + self.ScrollPanel.Padding.t
	else
		control.FinalPosition.y		= self.ScrollPanel.Padding.t
		control.FinalPosition.x		= ( self.ControlMaxSize.w + self.Spacing ) * ( index - 1 ) + self.ScrollPanel.Padding.l
	end
end

/* Get the total size of the contents */
BelListBox.GetContentsSize			= function( self )

	/* Calculate total size of contents */
	local size						= 0
	if self.Direction == LIST_DIR_V then
		size						= ( self.ControlMaxSize.h + self.Spacing ) * table.Count( self.ListItems )
	else
		size						= ( self.ControlMaxSize.w + self.Spacing ) * table.Count( self.ListItems )
	end
	
	return size
end

/* Get percent of contents that are visible */
BelListBox.GetPercentVisible		= function( self )

	/* Variables */
	local size						= self:GetContentsSize()
	local w							= self.Contents:GetMaxWide() - self.Contents.Padding.l - self.Contents.Padding.r
	local h							= self.Contents:GetMaxTall() - self.Contents.Padding.t - self.Contents.Padding.b
	
	/* Return full if contents size is smaller than the size of the list */
	if	( self.Direction == LIST_DIR_H and size <= w ) or
		( self.Direction == LIST_DIR_V and size <= h ) then
		return 1
	end
		
	/* Otherwise, calculate difference */
	if self.Direction == LIST_DIR_H then
		return w / size
	else
		return h / size
	end
end

/* Change the direction */
BelListBox.SetDirection				= function( self, value )
	
	/* Invalid direction */
	if value != LIST_DIR_V and value != LIST_DIR_H then
		DEBUG:ErrMsg( "Invalid direction specified", "BelListBox.SetDirection" )
		return
	end
	
	/* Nothing else if direction hasn't changed */
	if self.Direction == value then return end
	
	/* Change the direction */
	self.Direction					= value
	
	/* Update the layout based on the new direction */
	self:UpdateLayout()
	
	/* Update the direction of the scrollbar */
	self.Scrollbar:SetDirection( value )
end

/* Register the control */
vgui.Register( "BelListBox", BelListBox, "BelFrame" )